﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;

using System;

namespace Holla.Infrastructure.TestBase
{
    public static class InMemoryDbContextFactory
    {
        public static T Create<T>() where T : DbContext
        {
            var options = new DbContextOptionsBuilder<T>()
                .UseInMemoryDatabase(Guid.NewGuid().ToString())
                .ConfigureWarnings(x => x.Ignore(InMemoryEventId.TransactionIgnoredWarning))
                .Options;

            var dbContext = (T)Activator.CreateInstance(typeof(T), options);
            dbContext.Database.EnsureCreated();

            return dbContext;
        }

        public static void Destroy<T>(T dbContext) where T : DbContext
        {
            dbContext.Database.EnsureDeleted();
            dbContext.Dispose();
        }
    }
}
